<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

	<xsl:template match="glossary">
		<xsl:copy>
			<xsl:apply-templates select="@*"/>
			<xsl:apply-templates select="title"/>
			<xsl:apply-templates select="glossentry" mode="div">
					<xsl:sort select="translate(glossterm, 'azertyuiopqsdfghjklmwxcvbn', 'AZERTYUIOPQSDFGHJKLMWXCVBN' )"/>
			</xsl:apply-templates>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="glossentry" mode="div">
		<xsl:variable name="letter" select="translate(substring(glossterm, 1, 1), 'azertyuiopqsdfghjklmwxcvbn', 'AZERTYUIOPQSDFGHJKLMWXCVBN' )"/>
		<xsl:if test="not(preceding-sibling::glossentry[translate(substring(glossterm, 1, 1), 'azertyuiopqsdfghjklmwxcvbn', 'AZERTYUIOPQSDFGHJKLMWXCVBN' )=$letter])">
			<glossdiv id="gloss-{$letter}">
				<title>
					<xsl:value-of select="$letter"/>
				</title>
				<xsl:apply-templates select="parent::*/glossentry[translate(substring(glossterm, 1, 1), 'azertyuiopqsdfghjklmwxcvbn', 'AZERTYUIOPQSDFGHJKLMWXCVBN' )=$letter]">
					<xsl:sort select="translate(glossterm, 'azertyuiopqsdfghjklmwxcvbn', 'AZERTYUIOPQSDFGHJKLMWXCVBN' )"/>
				</xsl:apply-templates>
			</glossdiv>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="*|@*">
		<xsl:copy>
			<xsl:apply-templates select="@*"/>
			<xsl:apply-templates/>
		</xsl:copy>
	</xsl:template>

</xsl:stylesheet>
